/*
 * Copyright 2008 Nathan C Jones
 * 
 * This file is part of Fivehundred.
 * 
 * Fivehundred is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Fivehundred is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Fivehundred. If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.fivehundred.model;

import com.googlecode.fivehundred.svg.FaceCardRenderer;
import com.googlecode.fivehundred.svg.NumberCardRenderer;

/**
 * A default card implementation. The card's name will be derived from its face
 * and suit.
 * 
 * @author Nathan C Jones
 */
public class DefaultCard implements Card {

	/** The card's suit. */
	private final Suit suit;

	/** The card's face. */
	private final Face face;

	/** The card's position within its deck. */
	private final int value;

	/** The card's renderer. */
	private final CardRenderer renderer;

	/**
	 * Create a card with the given suit and face.
	 * 
	 * @param suit the suit of the card.
	 * @param face the face of the card.
	 * @param value the value of the card within its deck.
	 * @throws IllegalArgumentException if the suit or face are
	 *         <code>null</code>.
	 */
	// TODO card values should be stored in the deck, not the card.
	public DefaultCard(final Suit suit, final Face face, final int value) {
		if (suit == null) {
			throw new IllegalArgumentException("Cannot create a card with a null suit.");
		}
		if (face == null) {
			throw new IllegalArgumentException("Cannot create a card with a null face.");
		}
		this.suit = suit;
		this.face = face;
		this.value = value;
		if (face.getNumber() <= 10) {
			renderer = new NumberCardRenderer(this);
		} else {
			renderer = new FaceCardRenderer(this);
		}

	}

	/**
	 * {@inheritdoc}
	 */
	public Face getFace() {
		return face;
	}

	/**
	 * Returns the name of the card consisting of the card face, " of ", and the
	 * card suit, eg: "King of Hearts".
	 */
	public String getName() {
		return getFace() + " of " + getSuit();
	}

	/**
	 * {@inheritdoc}
	 */
	public Suit getSuit() {
		return suit;
	}

	public int compareTo(final Card o) {
		return o.getValue() - value;
	}

	public int getValue() {
		return value;
	}

	/**
	 * Delegates to {@link #getName()}.
	 */
	@Override
	public String toString() {
		return getName();
	}

	/**
	 * {@inheritdoc}
	 */
	public CardRenderer getCardRenderer() {
		return renderer;
	}

}
